<?php if (!defined("_ECRIRE_INC_VERSION")) return;

require_once(_DIR_PLUGIN_ECATALOGUE.'classes/class.ArticleOptionModel.php');
 
function formulaires_ecatalogue_article_option_charger_dist($id_article) {
	$valeurs = array();
    
    $ao_mdl = new ArticleOptionModel();   
    
    $connections = $ao_mdl->select('id_article='.$id_article); 
     
    foreach ($connections as $connection) {
        $valeurs["options[".$connection['id_option']."]"] = $connection['id_option'];    
    }
    
    //$valeurs['ddd[1]'] = '!!!!!!!!!';
    
	return $valeurs;
}


function formulaires_ecatalogue_article_option_verifier_dist($id_article){
    $erreurs = array();
	return $erreurs;
}


function formulaires_ecatalogue_article_option_traiter_dist($id_article) {
    $result = array();
    
    // Here try/catch is for future...
    try
    {
        $ao_mdl = new ArticleOptionModel();
        // First delete all connections
        $ao_mdl->delete('id_article='.$id_article);    
            
        // Then add all new connections
        $checkboxes = _request('checkboxes');
        if (isset($checkboxes) && $checkboxes && is_array($checkboxes)) {
            foreach ($checkboxes as $id_option=>$id_option) {
                if ($id_option)
                    $ao_mdl->insert(array('id_article'=>$id_article, 'id_option'=>$id_option));
            }
        }
        
        $selects = _request('selects');
        if (isset($selects) && $selects && is_array($selects)) {
            foreach ($selects as $id_groupe=>$id_option) {
                if ($id_option)
                    $ao_mdl->insert(array('id_article'=>$id_article, 'id_option'=>$id_option));
            }
        }
        
        $radios = _request('radios');
        if (isset($radios) && $radios && is_array($radios)) {
            foreach ($radios as $id_groupe=>$id_option) {
                if ($id_option)
                    $ao_mdl->insert(array('id_article'=>$id_article, 'id_option'=>$id_option));
            }
        }
        
        $result['message_ok'] = _T('ecatalogue:form_saved');
    } catch (Exception $e) {
        //var_dump($e->getMessage());
        $result['message_erreur'] = _T('ecatalogue:form_error');
    }
    
    return $result;     
}

?>